import _ from 'lodash';
import {useCommonStore} from "@jzl/stores";

export const isPlatform = (data) => {
  return data && data.id === -100;
};

export const isGlobal = (data) => {
  return data && data.id === -1;
};

export const isApp = (data) => {
  return data && (data.isApp || data.nodeType === 'app');
};

export const isFuncModule = (data) => {
  return data && data.func_module_id > 0;
};

export const canEdit = (data) => {
  let editing = true;

  if (!data || data.project_id === -1 || data.id === -100) {
    editing = false;
  }

  return editing;
};

export const hasFuncModuleEdit = (data, loginUser) => {
  return (data && data.func_module_id > 0) && (_.get(data, 'type') !== 1 || useCommonStore()?.loginUser?.type === '_SUPER');
};

export const hasModuleCreatePermission = (data, loginUser, project) => {
  return isGlobal(data) || isApp(data) || (isPlatform(data) && useCommonStore()?.loginUser?.type === '_SUPER');
};

export const isTask = (data) => {
  return data && data.task_id > 0;
};

export const hasTaskEdit = (data) => {
  return (data && data.task_id > 0) && (_.get(data, 'type') !== 1 || useCommonStore()?.loginUser?.type === '_SUPER');
};

export const hasTaskCreatePermission = (data) => {
  return isGlobal(data) || isApp(data) || (isPlatform(data) && useCommonStore()?.loginUser?.type === '_SUPER');
};

export const getTaskStorePath = (task) => {
  let taskCode    = _.get(task, "task_code") || "";
  let funcStore   = _.get(task, "func_store");
  let type        = _.get(task, "type");
  let appCode     = _.get(task, "app.group_code") || "";
  let projectCode = useCommonStore()?.project?.project_domain;
  let prefix = "";

  if (!taskCode || funcStore !== 3) return "任务存储不正确！";

  if (type === 1) {
    prefix = "/_task/";
  } else if (type === 2) {
    prefix = `/${projectCode}/_task/`;
  } else {
    prefix = `/${projectCode}/${appCode}/task/`;
  }

  return `${prefix}${taskCode}.js`;
};
